package Offer.offer;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 两个队列实现栈
 * @author leiwenkang
 * @date 2020/4/6
 */
public class Test07b两个队列实现栈 {
    Queue<Integer> list1 = new LinkedList<>();
    Queue<Integer> list2 = new LinkedList<>();

    public void push(int value) {
       if (!list2.isEmpty()) {
           list2.add(value);
       } else {
           list1.add(value);
       }
    }

    public int remove() {
        if (!list1.isEmpty()) {
            while (list1.size() > 1) {
                list2.add(list1.remove());
            }
            return list1.remove();
        } else if (!list2.isEmpty()) {
            while (list2.size() > 1) {
                list1.add(list2.remove());
            }
            return list2.remove();
        } else {
            throw new RuntimeException("栈为空！");
        }
    }



}
